package com.hoshiicloud.fileserver.util;

/**
 * 图片处理工具类
 * 
 * @author Mr.wu
 *
 */
public class ObsPictureProcessUtil {

	/**
	 * 允许的图片类型
	 */
	public final static String[] allowPicTypes = new String[] { ".png", ".jpg", ".bmp" };

	/**
	 * 缩放图片到指定尺寸
	 * 
	 * @param url
	 * @param width
	 * @param height
	 * @return
	 */
	public static String resize(String url, int width, int height) {
		return url + "/resize," + "h_" + height + ",w_" + width;

	}

	/**
	 * 转换图片到指定格式
	 * 
	 * @param url
	 * @param targetFormat
	 * @return
	 */
	public static String formatTransform(String url, String targetFormat) {
		return url + "/format," + targetFormat;
	}

	/**
	 * 压缩图片质量
	 * 
	 * @param url
	 * @param quality 相对质量值,取值范围1~100
	 * @return
	 */
	public static String compQuality(String url, int quality) {
		return url + "/quality,q_" + quality;
	}

	/**
	 * 判断字符串是否在字符数组中出现
	 * 
	 * @param contentType
	 * @param allowTypes
	 * @return
	 */
	public static boolean isValid(String contentType, String... allowTypes) {
		if (null == contentType || "".equals(contentType)) {
			return false;
		}
		for (String type : allowTypes) {
			if (contentType.indexOf(type) > -1) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断url是否为一个图片文件路径
	 * 
	 * @param url
	 * @return
	 */
	public static boolean isImg(String url) {
		return isValid(url, allowPicTypes);
	}

}
